/**
 * @author helio
 */
var PurchaseRefundBillSearchView = Ext.extend(BasicPanel,{
		
	initlize : function() {
		if(!this.inputForm) {
			this.inputForm = new Ext.FormPanel({
				region:'north',
				margins: '5 0 5 0',
				bodyStyle : 'background-color: transparent;', 
				autoHeight: true,
				border: false,		
				defaultType: 'textfield',
								
				items: [{
					xtype:'fieldset',
					title: '查询条件',
					autoHeight:true,
					anchor:'100%',
					layout:'column',
									
					items: [{
						columnWidth:.55,
						layout: 'form',
						labelWidth: 55,
						border:false,
						bodyStyle: 'padding-left:5px;background-color: transparent;',
						items: [{
							layout: 'column',
							fieldLabel: '编号',
							autoHeight:true,
							border: false,
							bodyStyle : 'background-color: transparent;',
							anchor:'95%',
							items: [{
								columnWidth:.46,
								xtype:'textfield',
							    name: 'code_from'
							},{ 
								columnWidth:.08,
								layout: 'fit',
								border: false,
								bodyStyle: 'padding-left:3px;text-align:center;background-color: transparent;',
							    items: {
									xtype:'label',					
									text: '～'
								}
							},{
								columnWidth:.46,
							    xtype:'textfield',
							    name: 'code_to'
							}]
						}, {
		                	xtype:'trigger',
		                    triggerClass: 'x-form-search-trigger',
		                    name: 'client',
		                    fieldLabel: '收货单位',
		                    anchor:'95%',
		                    onTriggerClick: function(){
								var name = this.inputForm.form.findField("client").getValue();
			   					var condition = {querytype:2,queryvalue:name};
			   					SALE.window.showManufacturerSelectWindow(this.clientSelectedHandler.createDelegate(this),condition);
				       		}.createDelegate(this)
		                },{
				            xtype: 'radiogroup',
				            fieldLabel: '状态',
				            items: [
				                {boxLabel: '未确认', name: 'status', inputValue: 0, checked: true},
				                {boxLabel: '已确认', name: 'status', inputValue: 1},
				                {boxLabel: '所有', name: 'status', inputValue: 2}
				            ]
				        }]
					},{
						columnWidth:.45,
						layout: 'form',
						labelWidth: 55,
						border:false,
						bodyStyle : 'background-color: transparent;',
						items: [{
							layout: 'column',
							fieldLabel: '录单日期',
							autoHeight: true,
							border: false,
							bodyStyle : 'background-color: transparent;',
							anchor:'100%',
							items: [{
								columnWidth:.46,
								xtype:'datefield',
								format:'Y-m-d',
							    name: 'entry_date_from'
							},{ 
								columnWidth:.08,
								layout: 'fit',
								border: false,
								bodyStyle: 'padding-left:4px;text-align:center;background-color: transparent;',
							    items: {
									xtype:'label',							
									text: '～'
								}
							},{
								columnWidth:.46,
							    xtype:'datefield',
								format:'Y-m-d',
							    name: 'entry_date_to'
							}]
						}, new Ext.form.ComboBox({
							tpl: comboxTpl,
							fieldLabel: '经办人',
					        store: DataStoreUtil.getUserStore(),
					        displayField:'name',
					        typeAhead: true,
					        mode: 'local',
					        forceSelection: false,
					        triggerAction: 'all',
					        //emptyText:'选择或输入经办人...',
					        selectOnFocus:true,
							name: 'operator',
							
							anchor:'100%'
					    })]
					}]
				}],
								
				buttons: [{
					text: '查询',
					listeners: {
						"click": this.searchClickHandler.createDelegate(this, [])
					}
				},{
					text: '取消',
					listeners:{
						"click":this.clearForm.createDelegate(this)
					}
				}]
			});
		}
		
		if(!this.detailsGrid) {
								
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.data.MemoryProxy(this.detailsJson),
			    reader: new Ext.data.JsonReader({
					root: 'details',
					remoteSort: true,
		            fields: ['id', 'code','client', 'amount', 'account', 'received', {name: 'entry_date', type: 'date'}, 'operator', 'comment', {name: 'confirm_status',type: 'bool'}]
				})
			});
			
			var csm = new Ext.grid.CheckboxSelectionModel();
			
			this.detailsGrid = new Ext.grid.GridPanel({
				region:'center',
				store: this.detailsStore,
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
						        
				stripeRows: true,
				border: true,
				autoExpandColumn: 'comment',
				
				columns: [new Ext.grid.RowNumberer(),{
	           		header: "编号",
	           		dataIndex: 'code',
	           		width: 120,
					sortable: true
	       		},{
	           		header: "收货单位",
	           		dataIndex: 'client',
	           		width: 70,
					sortable: true,
	           		align: 'left'
	        	},{
	           		header: "经办人",
	           		dataIndex: 'operator',
	           		width: 60,
	           		align: 'left',
					sortable: true
	           		
	        	},{
	           		header: "录单日期",
	           		dataIndex: 'entry_date',
	           		width: 70,
	           		align: 'right',
	           		renderer: Ext.util.Format.dateRenderer('Y-m-d'),
					sortable: true
	           		
	        	},{
	           		header: "收款金额",
	           		dataIndex: 'amount',
	           		width: 70,
	           		align: 'right',
					sortable: true
	           		
	        	},{
	           		header: "备注",
	           		dataIndex: 'comment',
	           		align: 'left',
					sortable: true
	           		
	        	},{
		            header: '状态',
		            dataIndex: 'confirm_status',
		            align: 'center',
		            width: 50,
		            renderer: SALE.Utils.confirmStatusRenderer.createDelegate(this)
		        }, csm],
				
				sm: csm,
				
				listeners: {
					cellclick : function(grid, rowIndex, columnIndex, e) {
						this.selectedRowIndex = rowIndex;
					}.createDelegate(this)
				},
				
				bbar:[{
		            text:'确认',
		            tooltip:'确认采购单信息',
		            iconCls:'accept',
					handler: this.confirmSaleRedundBillHandler.createDelegate(this)
		        }, '-', {
		            text:'查看详细',
		            tooltip:'查看、修改采购单信息',
		            iconCls:'edit',
					handler: this.checkSaleRedundBillHandler.createDelegate(this)
		        },'-', {
		            text:'删除',
		            tooltip:'删除选中的采购单',
		            iconCls:'remove',
					handler: this.deleteSaleRefundBillHandler.createDelegate(this)
		        }]				
			});
		}
	},
	
	getCorePanel : function() {
		this.corePanel = new Ext.Panel({
			title: '采购退货单查询',
			border: false,
			layout: 'border',
			
			items: [this.inputForm, this.detailsGrid]
		});

		return this.corePanel;
	},
	
	clientSelectedHandler: function(record) {
		this.inputForm.form.findField("client").setValue(record.get("name"));
	},
	
	searchClickHandler : function() {
		//show the load mask.
		SALE.Utils.showLoadMask(this.corePanel);
		
		this.condition = this.inputForm.form.getValues();
		this.condition.type = '1';
		SALE.Action.searchSaleRefundBill(this.refreshGridByArray.createDelegate(this), this.condition);
	},
	
	confirmSaleRedundBillHandler : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			this.warnMessage('请先选择采购退货单！！！', '警告信息');
		} else {
			this.confirmMessage('选择"确定"按钮对数据进行确定！！！', '提示信息', function(btn){
				if (btn == "ok") {
					var confirmArray = [];					
					for (var i = 0; i < recordList.length; i++) {
						var record = recordList[i];
						record.set("confirm_status", 1);
						
						var updateJson = record.data;
						updateJson.confirm_status='1';
						
						confirmArray.push(updateJson);
					}

					SALE.Action.saveSaleRefundBillInfo(this.saveSuccessHandler.createDelegate(this), confirmArray, "confirm");
				}
			}.createDelegate(this));
		} 
		
	},
	
	checkSaleRedundBillHandler : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			this.warnMessage('请先选择一条数据！！！', '警告信息');
		} else if(recordList.length > 1) {
			this.warnMessage('每次只能编辑一条数据，请确认只选中了一条数据！！！', '警告信息');
		} else {
			var id = recordList[0].get("id");
			
			SALE.Action.searchSaleRefundBillByID(PurchaseRefundBillInputView.showInputPanel, id);
		}		
	},
	
	deleteSaleRefundBillHandler : function() {
		this.selectedRecordList = this.detailsGrid.getSelectionModel().getSelections();
		if(this.selectedRecordList.length == 0) {
			this.warnMessage('请先选择一条数据！！！', '警告信息');
		} else {
			this.confirmMessage('删除后，数据无法恢复。<br/><br/>还要继续删除选中采购退货单吗？', '提示信息', function(btn){
				if (btn == "ok") {
					SALE.Action.deleteSaleRefundBillInfo(this.deleteSuccessHandler.createDelegate(this), this.selectedRecordList);
				}
			}.createDelegate(this));
		} 
	}
});

PurchaseRefundBillSearchView.showSearchPanel = function() {
	var searchView = new PurchaseRefundBillSearchView();
	searchView.initlize();
	SALE.App.showOperateTabPanel(searchView.getCorePanel(), "采购退货单查询");
}
